package com.demo.ui;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.filechooser.FileSystemView;
import javax.swing.tree.DefaultMutableTreeNode;

import com.demo.data.Configuration;
import com.demo.data.FileNode;

public class MainView extends JFrame {

	private static final long serialVersionUID = 7282021412312527649L;

	private JLabel mFilePath;
	private TreeView mExplorerTree;
	// private JTextArea mContent;
	private JTextArea mConsole;
	private JMenuBar mMenuBar;

	private JTabbedPane mTabbedPane;
	private List<JTextArea> mContentList;
	private List<FileNode> mFileList;

	private OnMenuItemClickListener mNewFileClickListener;
	private OnMenuItemClickListener mOpenFileClickListener;
	private OnMenuItemClickListener mSaveClickListener;
	private OnMenuItemClickListener mExitClickListener;

	private OnMenuItemClickListener mCopyClickListener;
	private OnMenuItemClickListener mPasteClickListener;

	private OnMenuItemClickListener mHidePathClickListener;
	private OnMenuItemClickListener mHideConsoleClickListener;

	private OnMenuItemClickListener mPerferencesClickListener;

	private OnMenuItemClickListener mNewProjectClickListener;
	private OnMenuItemClickListener mOpenProjectClickListener;

	private OnMenuItemClickListener mRunClickListener;
	private OnMenuItemClickListener mDebugClickListener;

	private OnMenuItemClickListener mAboutClickListener;

	private MainView(FileNode node) {
		super("Low Java Editor");
		this.setLayout(new BorderLayout());
		this.setSize(800, 640);
		Container container = getContentPane();
		mExplorerTree = new TreeView(node);
		mExplorerTree.setPreferredSize(new Dimension(240, 480));
		container.add(mExplorerTree, BorderLayout.WEST);

		mFilePath = new JLabel(node.getFile() == null ? node.getName() : node.getFile().getAbsolutePath());
		mFilePath.setBorder(new EmptyBorder(10, 10, 10, 10));
		container.add(mFilePath, BorderLayout.NORTH);

		mTabbedPane = new JTabbedPane();
		mTabbedPane.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				
			}
		});
		mContentList = new ArrayList<>();
		mFileList = new ArrayList<>();
		container.add(mTabbedPane, BorderLayout.CENTER);

		mConsole = new JTextArea();
		mConsole.setEditable(false);
		mConsole.setRows(10);
		mConsole.setBorder(new EmptyBorder(10, 10, 10, 10));
		container.add(new JScrollPane(mConsole), BorderLayout.SOUTH);

		initMenuBar();
		this.setJMenuBar(mMenuBar);
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}

	private void initMenuBar() {
		mMenuBar = new JMenuBar();

		JMenu fileMenu = new JMenu("File");
		JMenu editMenu = new JMenu("Edit");
		JMenu viewMenu = new JMenu("View");
		JMenu settingMenu = new JMenu("Setting");
		JMenu projectMenu = new JMenu("Project");
		JMenu compileMenu = new JMenu("Compile");
		JMenu helpMenu = new JMenu("Help");

		mMenuBar.add(fileMenu);
		mMenuBar.add(editMenu);
		mMenuBar.add(viewMenu);
		mMenuBar.add(settingMenu);
		mMenuBar.add(projectMenu);
		mMenuBar.add(compileMenu);
		mMenuBar.add(helpMenu);

		JMenuItem newFileMenuItem = new JMenuItem("New File");
		JMenuItem openFileMenuItem = new JMenuItem("Open File");

		JMenuItem exitMenuItem = new JMenuItem("Exit");
		fileMenu.add(newFileMenuItem);
		fileMenu.add(openFileMenuItem);
		fileMenu.addSeparator();
		fileMenu.add(exitMenuItem);

		newFileMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				DefaultMutableTreeNode parent = (DefaultMutableTreeNode) mExplorerTree.getTree()
						.getLastSelectedPathComponent();
				String name = JOptionPane.showInputDialog(null, "New a file", "Info", JOptionPane.PLAIN_MESSAGE);
				if (name == null || name.isEmpty()) {
					return;
				}
				FileNode pNode = (FileNode) parent.getUserObject();
				String childPath = pNode.getFile().getAbsolutePath() + File.separator + name;
				FileNode node = new FileNode();
				node.setName(name);
				node.setFile(new File(childPath));
				node.setDummyRoot(false);
				DefaultMutableTreeNode child = new DefaultMutableTreeNode(node, false);
				mExplorerTree.getTreeModel().insertNodeInto(child, parent, parent.getChildCount());
				if (mNewFileClickListener != null) {
					mNewFileClickListener.onClick(node);
				}
			}
		});

		JMenuItem saveMenuItem = new JMenuItem("Save");
		JMenuItem copyMenuItem = new JMenuItem("Copy");
		JMenuItem pasteMenuItem = new JMenuItem("Paste");
		editMenu.add(saveMenuItem);
		editMenu.add(copyMenuItem);
		editMenu.add(pasteMenuItem);
		saveMenuItem.setAccelerator(KeyStroke.getKeyStroke("ctrl S"));
		saveMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if (mSaveClickListener != null) {
					mSaveClickListener.onClick(mFileList.get(mTabbedPane.getSelectedIndex()));
				}
			}
		});

		JMenuItem hidePathMenuItem = new JCheckBoxMenuItem("Hide Path", false);
		JMenuItem hideConsoleMenuItem = new JCheckBoxMenuItem("Hide Console", false);
		viewMenu.add(hidePathMenuItem);
		viewMenu.add(hideConsoleMenuItem);

		JMenuItem perferencesMenuItem = new JMenuItem("Perferences");
		settingMenu.add(perferencesMenuItem);

		JMenuItem newProjectMenuItem = new JMenuItem("New Project");
		JMenuItem openProjectMenuItem = new JMenuItem("Open Project");
		projectMenu.add(newProjectMenuItem);
		projectMenu.add(openProjectMenuItem);

		newProjectMenuItem.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				FileSystemView fileSystemView = FileSystemView.getFileSystemView();
				DefaultMutableTreeNode parent = (DefaultMutableTreeNode) mExplorerTree.getTreeModel().getRoot();
				String name = JOptionPane.showInputDialog(null, "New a project", "Info", JOptionPane.PLAIN_MESSAGE);
				if (name == null || name.isEmpty()) {
					return;
				}
				String childPath = Configuration.getInstance().getWorkspace() + File.separator + name;
				FileNode node = new FileNode();
				File file = new File(childPath);
				node.setName(name);
				node.setFile(file);
				node.setIcon(fileSystemView.getSystemIcon(file));
				node.setDummyRoot(false);
				DefaultMutableTreeNode child = new DefaultMutableTreeNode(node, true);
				mExplorerTree.getTreeModel().insertNodeInto(child, parent, parent.getChildCount());
				if (mNewProjectClickListener != null) {
					mNewProjectClickListener.onClick(node);
				}
			}
		});

		JMenuItem runProjectMenuItem = new JMenuItem("Run");
		JMenuItem debugProjectMenuItem = new JMenuItem("Debug");
		compileMenu.add(runProjectMenuItem);
		compileMenu.add(debugProjectMenuItem);

		JMenuItem aboutMenuItem = new JMenuItem("About");
		helpMenu.add(aboutMenuItem);

	}

	public JLabel getFilePath() {
		return mFilePath;
	}

	public void setFilePath(String filePath) {
		mFilePath.setText(filePath);
	}

	public TreeView getExplorerTree() {
		return mExplorerTree;
	}

	public void addTab(FileNode node) {
		JTextArea content = new JTextArea();
		mContentList.add(content);
		mFileList.add(node);
		mTabbedPane.add(node.getName(), new JScrollPane(content));
		mTabbedPane.setSelectedIndex(mFileList.size() - 1);
	}

	public void remove(int index) {
		mFileList.remove(index);
		mContentList.remove(index);
		mTabbedPane.remove(index);
	}

	public JTextArea getContentArea() {
		return mContentList.get(mTabbedPane.getSelectedIndex());
	}

	public String getContentText() {
		return mContentList.get(mTabbedPane.getSelectedIndex()).getText();
	}

	public JTextArea getConsole() {
		return mConsole;
	}

	public static MainView newInstance(FileNode project) {
		return new MainView(project);
	}

	public void setNewFileClickListener(OnMenuItemClickListener listener) {
		this.mNewFileClickListener = listener;
	}

	public void setOpenFileClickListener(OnMenuItemClickListener listener) {
		this.mOpenFileClickListener = listener;
	}

	public void setSaveClickListener(OnMenuItemClickListener listener) {
		this.mSaveClickListener = listener;
	}

	public void setExitClickListener(OnMenuItemClickListener listener) {
		this.mExitClickListener = listener;
	}

	public void setCopyClickListener(OnMenuItemClickListener listener) {
		this.mCopyClickListener = listener;
	}

	public void setPasteClickListener(OnMenuItemClickListener listener) {
		this.mPasteClickListener = listener;
	}

	public void setHidePathClickListener(OnMenuItemClickListener listener) {
		this.mHidePathClickListener = listener;
	}

	public void setHideConsoleClickListener(OnMenuItemClickListener listener) {
		this.mHideConsoleClickListener = listener;
	}

	public void setPerferencesClickListener(OnMenuItemClickListener listener) {
		this.mPerferencesClickListener = listener;
	}

	public void setNewProjectClickListener(OnMenuItemClickListener listener) {
		this.mNewProjectClickListener = listener;
	}

	public void setOpenProjectClickListener(OnMenuItemClickListener listener) {
		this.mOpenProjectClickListener = listener;
	}

	public void setRunClickListener(OnMenuItemClickListener listener) {
		this.mRunClickListener = listener;
	}

	public void setDebugClickListener(OnMenuItemClickListener listener) {
		this.mDebugClickListener = listener;
	}

	public void setAboutClickListener(OnMenuItemClickListener listener) {
		this.mAboutClickListener = listener;
	}

	public static interface OnMenuItemClickListener {

		void onClick(Object object);
	}
}
